package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @description:
 * @author: BlueSky
 * @create: 2021-10-07 15:25
 **/
@Configuration
public class MyMQConfig {

	@Bean
	public MessageConverter messageConverter() {
		return new Jackson2JsonMessageConverter();
	}


	// @RabbitListener(queues = "order.release.order.queue")
	// public void handle(Message message){
	//
	// }

	@Bean
	public Queue orderDelayQueue(){
		HashMap<String, Object> arguments = new HashMap<>();
		arguments.put("x-dead-letter-exchange","order-event-exchange");
		arguments.put("x-dead-letter-routing-key","order.release.order");
		arguments.put("x-message-ttl",60000);

		Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
		return queue;
	}

	@Bean
	public Queue orderReleaseOrderQueue(){
		Queue queue = new Queue("order.release.order.queue", true, false, false, null);
		return queue;
	}

	@Bean
	public Exchange orderEventExchange(){
		return new TopicExchange("order-event-exchange", true, false);
	}

	@Bean
	public Binding orderCreateOrderBingding(){
		return new Binding("order.delay.queue",
			Binding.DestinationType.QUEUE,
			"order-event-exchange",
			"order.create.order",
			null);
	}

	@Bean
	public Binding orderReleaseOrderBingding(){
		return new Binding("order.release.order.queue",
				Binding.DestinationType.QUEUE,
				"order-event-exchange",
				"order.create.order",
				null);
	}

	/**
	 * 订单释放直接和库存释放进行绑定
	 * 原本的订单释放是通过延时队列进行释放，现在为立即发消息，即关闭订单时发送消息进行库存释放
	 * @return
	 */
	@Bean
	public Binding orderReleaseOtherBingding(){
		return new Binding("stock.release.stock.queue",
				Binding.DestinationType.QUEUE,
				"order-event-exchange",
				"order.create.order.#",
				null);
	}


}
